package com.pyip.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyip.reggie.Entity.AddressBook;
import com.pyip.reggie.Entity.Category;
import com.pyip.reggie.Entity.Dish;
import com.pyip.reggie.Entity.DishFlavor;
import com.pyip.reggie.common.BaseContext;
import com.pyip.reggie.common.R;
import com.pyip.reggie.dto.DishDto;
import com.pyip.reggie.mapper.AddressBookMapper;
import com.pyip.reggie.mapper.DishMapper;
import com.pyip.reggie.service.AddressBookService;
import com.pyip.reggie.service.CategoryService;
import com.pyip.reggie.service.DishFlavorService;
import com.pyip.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.beancontext.BeanContext;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {


    @Override
    public R<AddressBook> addAddressBook(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrent());
        log.info("addressBook is {}",addressBook);
        return  baseMapper.insert(addressBook) == 1 ? R.success(addressBook) : R.error("添加地址失败，请稍后再试");
    }

    @Transactional
    @Override
    public R<AddressBook> setDefaultAddressBook(AddressBook addressBook) {
        // 将当前用用户的所有地址信息全设置为非默认，再将当前地址设置为默认
        log.info("addressBook is {}",addressBook);

        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrent());
        wrapper.set(AddressBook::getIsDefault,0);
        // 更新
        this.update(wrapper);

        addressBook.setIsDefault(1);
        // 设置默认地址
        return baseMapper.updateById(addressBook) == 1 ? R.success(addressBook) : R.error("更新地址失败");
    }

    @Override
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrent());
        wrapper.eq(AddressBook::getIsDefault,1);

        return baseMapper.selectOne(wrapper) == null ? R.error("查无信息") : R.success(baseMapper.selectOne(wrapper));
    }

    @Override
    public R<List<AddressBook>> getList(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrent());

        log.info("addressBook is {}",addressBook);
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(addressBook.getUserId() != null ,AddressBook::getUserId,addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);

        return R.success(baseMapper.selectList(wrapper));
    }


}
